/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.trap;

import com.lineage.server.GeneralThreadPool;
import com.lineage.server.model.L1HardinQuest;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1World;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.storage.TrapStorage;

/**
 * Class <code>L1TrodSwitch</code> 陷阱:踩踏开关.
 * 
 * @author jrwz
 * @version 2012-7-13下午05:14:49
 * @see com.lineage.server.model.trap
 * @since JDK1.7
 */
public final class L1TrodSwitch extends L1Trap {

    /**
     * 开关线程.
     */
    class SwitchThread implements Runnable {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            // 这考虑建立一个地图的实例
            // IF语句else构成 但他并不多
            try {
                final int mapId = L1TrodSwitch.this._trodFrom.getMapId();
                boolean onPc;
                L1Location loc;
                switch (L1TrodSwitch.this._switchId) {
                    case 1: // 过去的说话之岛地监2F　骷髅房间
                        if (!L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            return;
                        }
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isDeleteTransactionNow()) {
                            return;
                        }
                        L1HardinQuest
                                .getInstance()
                                .getActiveMaps(mapId)
                                .tereportEntrance(
                                        L1TrodSwitch.this._trodFrom,
                                        new L1Location(32787, 32821,
                                                L1TrodSwitch.this._trodFrom
                                                        .getMapId()), 5);
                        break;
                    case 2: // 过去的说话之岛地监2F-2
                        if (!L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            return;
                        }
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isAlreadyFirstSwitch()) {
                            return;
                        }
                        onPc = false;
                        loc = new L1Location(32666, 32817, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPc = true;
                            }
                        }
                        /*
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32668,32817,mapId);
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32668,32819,mapId);;
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32666,32819,mapId);
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         */
                        if (!onPc) {
                            return;
                        }
                        L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .onFirstSwitch();
                        // 32667 32818 9000,
                        // 32668 32817 9000
                        // 32668 32819 9000
                        // 32666 32819
                        break;
                    case 3: // 过去的说话之岛地监2F-1 灯光
                        if (!L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            return;
                        }
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isAlreadySecondSwitch()) {
                            return;
                        }
                        onPc = false;
                        loc = new L1Location(32712, 32793, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPc = true;
                            }
                        }
                        /*
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32703,32791,mapId);
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32710,32803,mapId);;
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32703,32800,mapId);
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         */
                        if (!onPc) {
                            return;
                        }
                        L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .onSecondSwitch();
                        // 上 32712 32793
                        // 左 32703 32791
                        // 右 32710 32803
                        // 下 32703 32800
                        //
                        break;
                    case 4: // 过去的说话之岛地监2F-2 白色魔法阵创建（入口）
                        if (!L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            return;
                        }
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isAlreadyPortal()) {
                            return;
                        }
                        onPc = false;
                        loc = new L1Location(32809, 32837, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPc = true;
                            }
                        }
                        /*
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32807,32837,mapId);
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32809,32839,mapId);;
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         * if(!onPc) return;
                         * onPc =false;
                         * loc.set(32807,32839,mapId);
                         * for(L1Object obj:L1World.getInstance().getVisiblePoint(loc,0)){
                         * if(obj instanceof L1PcInstance){
                         * onPc=true;
                         * }
                         * }
                         */
                        if (!onPc) {
                            return;
                        }
                        L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .onThirdSwitch();

                        /* End */
                        // 中心 32808 32838
                        // 上32809,32837
                        // 左32807,32837
                        // 右32809,32839
                        // 下32807,32839
                        break;
                    case 5: // 过去的说话之岛地监2F-3 4人踩上去出现黑魔法阵
                            // 星付与位置-1(左)･2(右)
                            // 上-1 32806 32863
                            // 上-2 32808 32864
                            // 左-1 32800 32864
                            // 左-2 32799 32866
                            // 右-1 32807 32870
                            // 右-2 32806 32872
                            // 下-1 32798 32872
                            // 下-2 32800 32873
                            // 魔法阵 32802 32868
                        if (!L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            return;
                        }
                        int onPc_cnt = 0;
                        final int[] onPlayerList = new int[8];
                        loc = new L1Location(32806, 32863, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPlayerList[0] = obj.getId();
                                onPc_cnt += 1;
                            }
                        }
                        loc.set(32808, 32864, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPlayerList[1] = obj.getId();
                                onPc_cnt += 1;
                            }
                        }
                        loc.set(32800, 32864, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPlayerList[2] = obj.getId();
                                onPc_cnt += 1;
                            }
                        }
                        loc.set(32799, 32866, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPlayerList[3] = obj.getId();
                                onPc_cnt += 1;
                            }
                        }
                        loc.set(32807, 32870, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPlayerList[4] = obj.getId();
                                onPc_cnt += 1;
                            }
                        }
                        loc.set(32806, 32872, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPlayerList[5] = obj.getId();
                                onPc_cnt += 1;
                            }
                        }
                        loc.set(32798, 32872, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPlayerList[6] = obj.getId();
                                onPc_cnt += 1;
                            }
                        }
                        loc.set(32800, 32873, mapId);
                        for (final L1Object obj : L1World.getInstance()
                                .getVisiblePoint(loc, 0)) {
                            if (obj instanceof L1PcInstance) {
                                onPlayerList[7] = obj.getId();
                                onPc_cnt += 1;
                            }
                        }
                        if (onPc_cnt > 0) {
                            // if(onPc_cnt>3){
                            L1HardinQuest.getInstance().getActiveMaps(mapId)
                                    .setBlackRune(onPlayerList); // 标志
                        }
                        break;
                    case 6: // single 过去的说话之岛地监2F-4 踩踏黑魔法阵出现 之后瞬移
                            // 魔法阵 32802 32868
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            L1HardinQuest.getInstance().getActiveMaps(mapId)
                                    .onBlackRune();
                        }
                        break;
                    // 每次随机的红色标记是什么意思
                    case 7: // 「悄悄话传到了奥琳的耳朵里」
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            L1HardinQuest.getInstance().getActiveMaps(mapId)
                                    .sendWisperWindow(0); // 32684 32817
                        }
                        break;
                    case 8: // 「悄悄话传到了奥琳的耳朵里」
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            L1HardinQuest.getInstance().getActiveMaps(mapId)
                                    .sendWisperWindow(1); // 32732 32789
                        }
                        break;
                    case 9: // 「悄悄话传到了奥琳的耳朵里」
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            L1HardinQuest.getInstance().getActiveMaps(mapId)
                                    .sendWisperWindow(2); // 32760 32791
                        }
                        break;
                    case 10: // 「悄悄话传到了奥琳的耳朵里」
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            L1HardinQuest.getInstance().getActiveMaps(mapId)
                                    .sendWisperWindow(3); // 32729 32854
                        }
                        break;
                    case 11: // 「悄悄话传到了奥琳的耳朵里」
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            L1HardinQuest.getInstance().getActiveMaps(mapId)
                                    .sendWisperWindow(4); // 32667 32874
                        }
                        break;
                    case 12: // 过去的说话之岛地监2F　入口处
                        if (!L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isActive()) {
                            return;
                        }
                        if (L1HardinQuest.getInstance().getActiveMaps(mapId)
                                .isDeleteTransactionNow()) {
                            return;
                        }
                        L1HardinQuest
                                .getInstance()
                                .getActiveMaps(mapId)
                                .tereportEntrance(
                                        L1TrodSwitch.this._trodFrom,
                                        new L1Location(32666, 32797,
                                                L1TrodSwitch.this._trodFrom
                                                        .getMapId()), 5);
                        break;
                    default:
                        break;
                }
            } catch (final Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 序列版本UID.
     */
    private static final long serialVersionUID = 1L;
    /** 开关编号. */
    private final int _switchId;
    /** 来自踩踏. */
    private L1PcInstance _trodFrom;

    /**
     * 踩踏开关.
     * 
     * @param storage
     *            - 储存
     */
    public L1TrodSwitch(final TrapStorage storage) {
        super(storage);
        this._switchId = storage.getInt("switchId");
    }

    @Override
    public void onTrod(final L1PcInstance trodFrom, final L1Object trapObj) {
        this._trodFrom = trodFrom;
        GeneralThreadPool.getInstance().execute(new SwitchThread());
    }
}
